﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ZWZ.Web.WebAdmin.UCtrs
{
    public partial class UserPower : System.Web.UI.UserControl
    {
        public void SetPower(Codes.UserPower power)
        {
            this.Powers = power;
        }
        protected Codes.UserPower Powers { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RptPower.DataSource = Config.ConfigAdapter.SitePower.Groups;
                RptPower.DataBind();
            }
        }
        protected void RptPower_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Common.XmlConfig.ConfigGroup group = e.Item.DataItem as Common.XmlConfig.ConfigGroup;
                CheckBoxList CkbPower = e.Item.FindControl("CkbPower") as CheckBoxList;
                HyperLink LnkPower = e.Item.FindControl("LnkPower") as HyperLink;
                if (group.Settings.Count == 0 && !string.IsNullOrEmpty(group.Attrs["Url"].Value))
                {
                    CkbPower.Visible = false;
                    LnkPower.NavigateUrl = group.Attrs["Url"].Value + "?uid=" + Codes.LoginHelper.CurrentUser.ID;
                    LnkPower.Target = "_Blank";
                }
                else
                {
                    LnkPower.Visible = false;
                    CkbPower.DataSource = group.Settings;
                    CkbPower.DataTextField = "Text";
                    CkbPower.DataValueField = "Key";
                    CkbPower.DataBind();
                    if (Powers != null) { foreach (ListItem item in CkbPower.Items) item.Selected = Powers.CheckPower(group.Key, item.Value); }
                }
            }
        }
        public string GetPowerString()
        {
            Dictionary<string, string[]> powerdic = new Dictionary<string, string[]>();
            foreach (RepeaterItem item in RptPower.Items)
            {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                {
                    CheckBoxList CkbPower = item.FindControl("CkbPower") as CheckBoxList;
                    HyperLink LnkPower = item.FindControl("LnkPower") as HyperLink;
                    HiddenField HidPower = item.FindControl("HidPower") as HiddenField;
                    HiddenField HidGroupKey = item.FindControl("HidGroupKey") as HiddenField;

                    string groupkey = HidGroupKey.Value;
                    if (LnkPower.Visible)
                    {
                        powerdic.Add(groupkey, new string[] { HidPower.Value });
                    }
                    else
                    {
                        List<string> vals = new List<string>();
                        foreach (ListItem li in CkbPower.Items)
                        {
                            if (li.Selected) vals.Add(li.Value);
                        }
                        powerdic.Add(groupkey, vals.ToArray());
                    }
                }
            }
            return new Codes.UserPower(powerdic).PowerString;
        }
    }
}